package trafficgeneratorcr.gui;

import java.util.ArrayList;
import javax.swing.DefaultListModel;

public class MainFrame extends javax.swing.JFrame {

    private ArrayList<WeeklyDistPanel> weeklyDistPanelList;
    private DefaultListModel trafficTypeListModel;

    /** Creates new form MainFrame */
    public MainFrame() {
        trafficTypeListModel = new DefaultListModel();

        initComponents();

        weeklyDistPanelList = new ArrayList<WeeklyDistPanel>();

        setStatus("Idle");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        generateButton = new javax.swing.JButton();
        numberOfWeeksSpinner = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        trafficTypeList = new javax.swing.JList();
        addTrafficTypeButton = new javax.swing.JButton();
        removeTrafficTypeButton = new javax.swing.JButton();
        trafficTypeNameTextField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        oneHourGranRadioButton = new javax.swing.JRadioButton();
        thirtyMinGranRadioButton = new javax.swing.JRadioButton();
        fifteenMinGranRadioButton = new javax.swing.JRadioButton();
        jPanel4 = new javax.swing.JPanel();
        statusLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        typeTabbedPane = new javax.swing.JTabbedPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setPreferredSize(new java.awt.Dimension(180, 621));

        generateButton.setText("Generate");
        generateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonActionPerformed(evt);
            }
        });

        numberOfWeeksSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 1, 1000, 1));
        numberOfWeeksSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                numberOfWeeksSpinnerStateChanged(evt);
            }
        });

        jLabel1.setText("Number of Weeks:");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Traffic types"));

        trafficTypeList.setModel(trafficTypeListModel);
        trafficTypeList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        trafficTypeList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                trafficTypeListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(trafficTypeList);

        addTrafficTypeButton.setText("+");
        addTrafficTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTrafficTypeButtonActionPerformed(evt);
            }
        });

        removeTrafficTypeButton.setText("-");
        removeTrafficTypeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeTrafficTypeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(addTrafficTypeButton, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeTrafficTypeButton, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
                    .addComponent(trafficTypeNameTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(trafficTypeNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addTrafficTypeButton)
                    .addComponent(removeTrafficTypeButton))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Granularity"));
        jPanel3.setLayout(new java.awt.GridLayout(3, 1));

        buttonGroup1.add(oneHourGranRadioButton);
        oneHourGranRadioButton.setText("1 hour");
        jPanel3.add(oneHourGranRadioButton);

        buttonGroup1.add(thirtyMinGranRadioButton);
        thirtyMinGranRadioButton.setText("30 min");
        jPanel3.add(thirtyMinGranRadioButton);

        buttonGroup1.add(fifteenMinGranRadioButton);
        fifteenMinGranRadioButton.setText("15 min");
        jPanel3.add(fifteenMinGranRadioButton);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(generateButton, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(numberOfWeeksSpinner, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(numberOfWeeksSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 231, Short.MAX_VALUE)
                .addComponent(generateButton)
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.LINE_START);

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel4.setPreferredSize(new java.awt.Dimension(1036, 25));
        jPanel4.setLayout(new java.awt.BorderLayout());

        statusLabel.setPreferredSize(new java.awt.Dimension(180, 15));
        jPanel4.add(statusLabel, java.awt.BorderLayout.LINE_START);

        progressBar.setStringPainted(true);
        jPanel4.add(progressBar, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel4, java.awt.BorderLayout.PAGE_END);
        getContentPane().add(typeTabbedPane, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addTrafficTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTrafficTypeButtonActionPerformed
        String trafficTypeName = trafficTypeNameTextField.getText().trim();

        if (trafficTypeName.length() == 0)
            // TODO: show a message
            return;

        for (int i = 0; i < trafficTypeListModel.getSize(); i++)
            if (trafficTypeListModel.get(i).equals(trafficTypeName))
                // TODO: show a message
                return;

        // add type name to the JList
        trafficTypeListModel.addElement(trafficTypeName);

        // add a corresponding new tab
        WeeklyDistPanel weeklyDistPanel = new WeeklyDistPanel();
        weeklyDistPanelList.add(weeklyDistPanel);
        typeTabbedPane.add(trafficTypeName, weeklyDistPanel);

        // force a number of weeks update
        numberOfWeeksSpinnerStateChanged(null);
    }//GEN-LAST:event_addTrafficTypeButtonActionPerformed

    private void removeTrafficTypeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeTrafficTypeButtonActionPerformed
        int selectedIndex = trafficTypeList.getSelectedIndex();

        if (selectedIndex == -1)
            // TODO: show a message
            return;

        // remove it from the JList
        trafficTypeListModel.removeElementAt(selectedIndex);

        // remove the corresponding tab
        weeklyDistPanelList.remove(selectedIndex);
        typeTabbedPane.remove(selectedIndex);
    }//GEN-LAST:event_removeTrafficTypeButtonActionPerformed

    private void trafficTypeListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_trafficTypeListValueChanged
        String selectedTrafficTypeName = (String) trafficTypeList.getSelectedValue();
        trafficTypeNameTextField.setText(selectedTrafficTypeName);
    }//GEN-LAST:event_trafficTypeListValueChanged

    private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonActionPerformed
        int numberOfWeeks = ((Integer) numberOfWeeksSpinner.getValue()).intValue();
    }//GEN-LAST:event_generateButtonActionPerformed

    private void numberOfWeeksSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_numberOfWeeksSpinnerStateChanged
        int numberOfWeeks = ((Integer) numberOfWeeksSpinner.getValue()).intValue();

        for (int i = 0; i < weeklyDistPanelList.size(); i++){
            WeeklyDistPanel weeklyDistPanel = weeklyDistPanelList.get(i);
            weeklyDistPanel.setNumberOfWeeks(numberOfWeeks);
        }

    }//GEN-LAST:event_numberOfWeeksSpinnerStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addTrafficTypeButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton fifteenMinGranRadioButton;
    private javax.swing.JButton generateButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner numberOfWeeksSpinner;
    private javax.swing.JRadioButton oneHourGranRadioButton;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JButton removeTrafficTypeButton;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JRadioButton thirtyMinGranRadioButton;
    private javax.swing.JList trafficTypeList;
    private javax.swing.JTextField trafficTypeNameTextField;
    private javax.swing.JTabbedPane typeTabbedPane;
    // End of variables declaration//GEN-END:variables

    private double[][][] getDistributions() {
        
        double[][][] distributions =  new double[weeklyDistPanelList.size()][][];

        for (int i = 0; i < weeklyDistPanelList.size(); i++){
            WeeklyDistPanel weeklyDistPanel = weeklyDistPanelList.get(i);
            distributions[i] = weeklyDistPanel.getWeeklyDistributions();
        }
        
        return distributions;
    }

    private void setStatus(String status) {
        statusLabel.setText(" " + status);
    }

    private void setProgress(int progress) {
        this.progressBar.setValue(progress);
        this.progressBar.setString(progress + " %");
    }

}

